package crow.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import java.util.Calendar;

import crow.ui.MainActivity;

public class AlarmReceiver extends BroadcastReceiver {
    public static final String SP_ALARM_RECORD = "alarm_record";
    public static final String SP_ALARM_RECORD_TIME = "alarm_time";

    @Override
    public void onReceive(Context context, Intent arg1) {
        Log.d("AlarmReceiver", "call onReceive");

        SharedPreferences sp = context.getSharedPreferences(SP_ALARM_RECORD, Activity.MODE_PRIVATE);
        String hour = String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(Calendar.getInstance().get(Calendar.MINUTE));
        String time = sp.getString(SP_ALARM_RECORD_TIME, null);
        if (time != null) {
            Toast.makeText(context, "闹钟时间到", Toast.LENGTH_SHORT).show();
            context.startService(new Intent("su.crow.MUSIC"));

            // launch activity to receiver music stop broadcast
            Intent intent = new Intent(context, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

}